package com.bjpowernode.money.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.money.mapper.BidInfoMapper;
import com.bjpowernode.money.mapper.IncomeRecordMapper;
import com.bjpowernode.money.mapper.LoanInfoMapper;
import com.bjpowernode.money.model.BidInfo;
import com.bjpowernode.money.model.IncomeRecord;
import com.bjpowernode.money.model.LoanInfo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 收益业务实现类
 */
@Service(interfaceClass = IncomeRecordService.class,version = "1.0.0",timeout = 20000)
@Component
public class IncomeRecordServiceImpl implements IncomeRecordService {
    @Autowired
    LoanInfoMapper loanInfoMapper;

    @Autowired
    BidInfoMapper bidInfoMapper;

    @Autowired
    IncomeRecordMapper incomeRecordMapper;

    @Override
    @Transactional
    public void generatePlan() {
        /**
         * 1、查询产品表中 满标产品===》List
         *2、遍历List，获取每个满标产品
         * 3、根据每个满标产品 到投资记录表中 查询投资记录==》List
         * 4、遍历投资记录 ，逐条生成 收益计划
         */
        List<LoanInfo> loanInfoList=loanInfoMapper.selectLoanInfosByFullStatus();
        for(LoanInfo loanInfo:loanInfoList){
            List<BidInfo> bidInfoList=  bidInfoMapper.selectBidIfosByLoanId(loanInfo.getId());
            for(BidInfo bidInfo:bidInfoList){
                IncomeRecord incomeRecord=new IncomeRecord();
                incomeRecord.setBidId(bidInfo.getId());
                incomeRecord.setBidMoney(bidInfo.getBidMoney());
                incomeRecord.setIncomeStatus(0);
                incomeRecord.setLoanId(loanInfo.getId());
                incomeRecord.setUid(bidInfo.getUid());
                Date date=null;
                Double incomeMoney=null;
                if(loanInfo.getProductType()==0){
                    date= DateUtils.addDays( loanInfo.getProductFullTime(),loanInfo.getCycle());
                    incomeMoney=bidInfo.getBidMoney()*loanInfo.getRate()/100/365*loanInfo.getCycle();
                }else{
                    date= DateUtils.addMonths( loanInfo.getProductFullTime(),loanInfo.getCycle());
                    incomeMoney=bidInfo.getBidMoney()*loanInfo.getRate()/100/365*30*loanInfo.getCycle();
                }
                incomeRecord.setIncomeDate(date);
                DecimalFormat df = new DecimalFormat("#0.00");
                incomeRecord.setIncomeMoney(Double.parseDouble(df.format(incomeMoney)));
                incomeRecordMapper.insertSelective(incomeRecord);
            }
            loanInfo.setProductStatus(2);
            loanInfoMapper.updateByPrimaryKeySelective(loanInfo);
        }
   }

    public static void main(String[] args) {
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,5);
        System.out.println(calendar.getTime());
    }
}
